﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Packaging;
using System.ComponentModel;
using System.IO;

namespace Fireball.ImagePak
{
    /// <summary>
    /// The Image class is used for storing the binary data and for get and set the image
    /// information's
    /// </summary>
    public sealed class Image
    {
        private ZipPackagePart _PackagePart = null;
        
        internal Image(ZipPackagePart packagePart)
        {
            _PackagePart = packagePart;
        }

        public Stream GetStream()
        {
            return _PackagePart.GetStream();
        }

        /// <summary>
        /// Get The Information About the Image
        /// </summary>
        /// <returns></returns>
        public ImageInfo GetInfo()
        {
            var rel = _PackagePart.GetRelationship("info_" + this.ImageId.ToString());

            if (rel != null)
            {
                var part = _PackagePart.Package.GetPart(rel.TargetUri);

                return ImageInfo.Deserialize(part.GetStream());
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Set the information about this image, if the parameter is null
        /// the information will be removed from the package
        /// </summary>
        /// <param name="info"></param>
        public void SetInfo(ImageInfo info)
        {
            var relId = "info_" + this.ImageId.ToString();
            //do this if a relationship exist and the new info is null
            if (info == null)
            {
                PackageRelationship rel = _PackagePart.RelationshipExists(relId) ?
                    _PackagePart.GetRelationship(relId) : null;

                if (rel != null)
                {
                    var part = _PackagePart.Package.GetPart(rel.TargetUri);
                    part.Package.DeletePart(part.Uri);
                    rel.Package.DeleteRelationship(rel.Id);
                }
            }
            else
            {
                var rel = _PackagePart.RelationshipExists(relId) ?
                    _PackagePart.GetRelationship(relId) : null;

                PackagePart infoPart = null;
                if (rel == null)
                {
                    infoPart = _PackagePart.Package.CreatePart(new Uri("/Images/Info/" + this.ImageId.ToString() + ".xml", UriKind.Relative), "text/xml");
                    rel = _PackagePart.CreateRelationship(infoPart.Uri,
                         TargetMode.Internal, "infolink");
                }
                else
                {
                    infoPart = _PackagePart.Package.GetPart(rel.TargetUri);
                }

                ImageInfo.Serialize(info, infoPart.GetStream());
            }
        }
       
            

        #region Properties
        /// <summary>
        /// The Image Uri on the package
        /// </summary>
        public Uri PackageUri
        {
            get
            {
                return _PackagePart.Uri;
            }
        }

        /// <summary>
        /// The Id assigned to this image
        /// </summary>
        public Guid ImageId
        {
            get
            {
                var t = _PackagePart.Uri.OriginalString.Substring("/Images/Data/".Length);

                t = t.Substring(0, t.Length - 4);

                return new Guid(t);
            }
        }

        #endregion
    }
}
